package com.situ.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Teacher;
import com.situ.web.service.TeacherService;
import com.situ.web.util.JDBCUtil;



@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {
	private TeacherService teacherService = new TeacherService();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		
		String method = req.getParameter("method");
		System.out.println("method = "+method);
		if(method == null || "".equals(method)) {
			method = "findAll";
		}
		switch (method) {
		case "findAll":
			findAll(req, resp);
			break;
		case "deleteTeacher":
			deleteTeacher(req, resp);
			break;
		case "insertTeacher":
			insertTeacher(req, resp);
			break;
		case "selectId":
			selectId(req, resp);
			break;
		case "updateTeacher":
			updateTeacher(req, resp);
			break;

		default:
			break;
		}
	}
	
	//修改2：修改
	private void updateTeacher(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
		String id = req.getParameter("id");
		System.out.println("id = " + id);
		String name = req.getParameter("name");
		System.out.println("name = " + name);
		String age = req.getParameter("age");
		System.out.println("age = " + age);
		String gender = req.getParameter("gender");
		System.out.println("gender = " + gender);
		Teacher teacher = new Teacher(Integer.parseInt(id), name, Integer.parseInt(age), gender);
		
		boolean isSuccess = teacherService.updateTeacher(teacher);
		
		
		resp.sendRedirect(req.getContextPath()+"/teacher");
	}

	//修改1，查询Id
	private void selectId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String Selectid = req.getParameter("id");
		System.out.println("id = " + Selectid);
		
		Teacher teacher = teacherService.SelectById(Integer.parseInt(Selectid));
		
		req.setAttribute("teacher", teacher);
		req.getRequestDispatcher("teacher_update.jsp").forward(req, resp);
		
	}

	//添加
	private void insertTeacher(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
		String id = req.getParameter("id");
		System.out.println("id = " + id);
		String name = req.getParameter("name");
		System.out.println("name = " + name);
		String age = req.getParameter("age");
		System.out.println("age = " + age);
		String gender = req.getParameter("gender");
		System.out.println("gender = " + gender);
		Teacher teacher = new Teacher(Integer.parseInt(id), name, Integer.parseInt(age), gender);
		
		boolean isSuccess = teacherService.insertTeacher(teacher);
		
		resp.sendRedirect(req.getContextPath()+"/teacher");
	}

	//删除
	private void deleteTeacher(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
		String id = req.getParameter("id");
		System.out.println("id = " + id);
		
		boolean isSuccess = teacherService.deleteById(Integer.parseInt(id));
		
		resp.sendRedirect(req.getContextPath() + "/teacher");
	}

	//查找
	private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ArrayList<Teacher> list = teacherService.findAll();
		
		//在后台打印下数据确认	
		for (Teacher teacher : list) {
			System.out.println(teacher);
		}
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/teacher_list.jsp").forward(req, resp);
	}

}
